<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta name="Content-Type" content="text/html; charset=UTF-8" />
<title>Module: MasterMayI::ActiveRecordExtensions::ClassMethods</title>
<link rel="stylesheet" href="../../css/style.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="../../css/common.css" type="text/css" media="screen" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  relpath = '../..';
  if (relpath != '') relpath += '/';
</script>
<script type="text/javascript" charset="utf-8" src="../../js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/app.js"></script>

  </head>
  <body>
    <div id="header">
      <div id="menu">
  
    <a href="../../_index.html">Index (C)</a> &raquo; 
    <a href="../../MasterMayI.html" title="MasterMayI">MasterMayI</a> &raquo; <a href="../ActiveRecordExtensions.html" title="ActiveRecordExtensions">ActiveRecordExtensions</a>
     &raquo; 
    <span class="title">ClassMethods</span>
  
</div>

      <div id="search">
  <a id="class_list_link" href="#">Namespace List</a>
  <a id="method_list_link" href="#">Method List</a>
  <a id ="file_list_link" href="#">File List</a>
</div>

      <div class="clear"></div>
    </div>
    
    <iframe id="search_frame"></iframe>
    
    <div id="content"><h1>Module: MasterMayI::ActiveRecordExtensions::ClassMethods 
  
  
</h1>

<dl class="box">
  
  
    
  
    
  
  
    <dt class="r1 last">Defined in:</dt>
    <dd class="r1 last">lib/master_may_i/active_record_extensions.rb</dd>
  
</dl>
<div class="clear"></div>




  <h2>Method Summary</h2>
  <ul class="summary">
    
      <li class="public ">
  <span class="summary_signature"><a href="#creatable%3F-instance_method" title="#creatable? (instance method)">- (Object) <strong>creatable?</strong> </a>
  
  </span>
  
  
  
  

  
    <span class="summary_desc">
Should the current user be able to create a record? Delegates to
<tt>creatable_by?</tt>.

</span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature"><a href="#creatable_by%3F-instance_method" title="#creatable_by? (instance method)">- (Object) <strong>creatable_by?</strong>(user) </a>
  
  </span>
  
  
  
  

  
    <span class="summary_desc">
Should the given user be able to create a record? This method should be
redefined inside the model.

</span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature"><a href="#listable-instance_method" title="#listable (instance method)">- (Object) <strong>listable</strong> </a>
  
  </span>
  
  
  
  

  
    <span class="summary_desc">
Returns a named scope representing all records the current user should be
able to view.

</span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature"><a href="#listable_by-instance_method" title="#listable_by (instance method)">- (Object) <strong>listable_by</strong>(user) </a>
  
  </span>
  
  
  
  

  
    <span class="summary_desc">
Returns a named scope representing all records the given user should be
able to view.

</span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature"><a href="#records_creating_user-instance_method" title="#records_creating_user (instance method)">- (Object) <strong>records_creating_user</strong>(opts_hash = {}) </a>
  
  </span>
  
  
  
  

  
    <span class="summary_desc">
Record the <tt>user_from_session</tt> as the <tt>creator</tt> when creating
a new record.

</span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature"><a href="#user_from_session-instance_method" title="#user_from_session (instance method)">- (Object) <strong>user_from_session</strong> </a>
  
  </span>
  
  
  
  

  
    <span class="summary_desc">
Returns the currently logged in user via the Authlogic UserSession class.

</span>
  
</li>

    
  </ul>



  <div id="method_details">
    <h2>Method Details</h2>
    
      <div class="method_details first">
  <p class="signature first" id="creatable?-instance_method">
  
    - (<tt>Object</tt>) <strong>creatable?</strong> 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Should the current user be able to create a record? Delegates to
<tt>creatable_by?</tt>
</p>


  </div>
</div>
<div class="tags">
  
  <h3>See Also:</h3>
  <ul class="see">
    
      <li><a href="#creatable_by%3F-instance_method" title="creatable_by?">creatable_by?</a></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


89
90
91</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/master_may_i/active_record_extensions.rb', line 89</span>

<span class='def def kw'>def</span> <span class='creatable? fid id'>creatable?</span>
  <span class='creatable_by? fid id'>creatable_by?</span><span class='lparen token'>(</span><span class='user_from_session identifier id'>user_from_session</span><span class='rparen token'>)</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="creatable_by?-instance_method">
  
    - (<tt>Object</tt>) <strong>creatable_by?</strong>(user) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Should the given user be able to create a record? This method should be
redefined inside the model.
</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='type'>(<tt>User</tt>, <tt>nil</tt>)</span>
      
      
        <span class='name'>user</span>
      
      
      
        &mdash;
        
Either a user record, or nil for visitors.


      
    </li>
  
</ul>

  <h3>See Also:</h3>
  <ul class="see">
    
      <li><a href="../../ActiveSupport/TestCase.html#should_be_creatable_by-class_method" title="should_be_creatable_by">should_be_creatable_by</a></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


99
100
101</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/master_may_i/active_record_extensions.rb', line 99</span>

<span class='def def kw'>def</span> <span class='creatable_by? fid id'>creatable_by?</span><span class='lparen token'>(</span><span class='user identifier id'>user</span><span class='rparen token'>)</span>
  <span class='true true kw'>true</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="listable-instance_method">
  
    - (<tt>Object</tt>) <strong>listable</strong> 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Returns a named scope representing all records the current user should be
able to view. Delegates to <tt>listable_by</tt>
</p>
<p>
Note: This method is not a predicate, but returns a scope. Therefore, the
method name does not end in a question mark.
</p>


  </div>
</div>
<div class="tags">
  
  <h3>See Also:</h3>
  <ul class="see">
    
      <li><a href="#listable_by-instance_method" title="listable_by">listable_by</a></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


69
70
71</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/master_may_i/active_record_extensions.rb', line 69</span>

<span class='def def kw'>def</span> <span class='listable identifier id'>listable</span>
  <span class='listable_by identifier id'>listable_by</span><span class='lparen token'>(</span><span class='user_from_session identifier id'>user_from_session</span><span class='rparen token'>)</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="listable_by-instance_method">
  
    - (<tt>Object</tt>) <strong>listable_by</strong>(user) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Returns a named scope representing all records the given user should be
able to view. This method should be redefined inside the model.
</p>
<p>
Note: This method is not a predicate, but returns a scope. Therefore, the
method name does not end in a question mark.
</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='type'>(<tt>User</tt>, <tt>nil</tt>)</span>
      
      
        <span class='name'>user</span>
      
      
      
        &mdash;
        
Either a user record, or nil for visitors.


      
    </li>
  
</ul>

  <h3>See Also:</h3>
  <ul class="see">
    
      <li><a href="../../ActiveSupport/TestCase.html#should_be_returned_via_listable_by-class_method" title="should_be_returned_via_listable_by">should_be_returned_via_listable_by</a></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


82
83
84</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/master_may_i/active_record_extensions.rb', line 82</span>

<span class='def def kw'>def</span> <span class='listable_by identifier id'>listable_by</span><span class='lparen token'>(</span><span class='user identifier id'>user</span><span class='rparen token'>)</span>
  <span class='scoped identifier id'>scoped</span><span class='lparen token'>(</span><span class='lbrace token'>{</span><span class='rbrace token'>}</span><span class='rparen token'>)</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="records_creating_user-instance_method">
  
    - (<tt>Object</tt>) <strong>records_creating_user</strong>(opts_hash = {}) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Record the <tt>user_from_session</tt> as the <tt>creator</tt> when creating
a new record.
</p>
<p>
This adds a belongs_to association named :creator, the
<tt>@model.created_by?(user)</tt> method, and a before_validation_on_create
method that sets the user from the Authlogic session.
</p>


  </div>
</div>
<div class="tags">
  
  
    
    

  <h3>See Also:</h3>
  <ul class="see">
    
      <li><a href="#user_from_session-instance_method" title="user_from_session">user_from_session</a></li>
    
      <li><a href="../../ActiveSupport/TestCase.html#should_record_creating_user-class_method" title="should_record_creating_user">should_record_creating_user</a></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/master_may_i/active_record_extensions.rb', line 118</span>

<span class='def def kw'>def</span> <span class='records_creating_user identifier id'>records_creating_user</span><span class='lparen token'>(</span><span class='opts_hash identifier id'>opts_hash</span> <span class='assign token'>=</span> <span class='lbrace token'>{</span><span class='rbrace token'>}</span><span class='rparen token'>)</span>
  <span class='opts identifier id'>opts</span> <span class='assign token'>=</span> <span class='HashWithIndifferentAccess constant id'>HashWithIndifferentAccess</span><span class='dot token'>.</span><span class='new identifier id'>new</span><span class='lparen token'>(</span><span class='opts_hash identifier id'>opts_hash</span><span class='rparen token'>)</span>
  <span class='association_name identifier id'>association_name</span> <span class='assign token'>=</span> <span class='lparen token'>(</span><span class='opts identifier id'>opts</span><span class='lbrack token'>[</span><span class='symbol val'>:as</span><span class='rbrack token'>]</span> <span class='orop op'>||</span> <span class='symbol val'>:creator</span><span class='rparen token'>)</span><span class='dot token'>.</span><span class='to_sym identifier id'>to_sym</span>

  <span class='before_validation_on_create identifier id'>before_validation_on_create</span> <span class='symbol val'>:set_creating_user_from_session</span>
  <span class='unless unless kw'>unless</span> <span class='reflect_on_association identifier id'>reflect_on_association</span><span class='lparen token'>(</span><span class='association_name identifier id'>association_name</span><span class='rparen token'>)</span>
    <span class='if if kw'>if</span> <span class='association_name identifier id'>association_name</span> <span class='eq op'>==</span> <span class='symbol val'>:user</span>
      <span class='belongs_to identifier id'>belongs_to</span> <span class='symbol val'>:user</span>
    <span class='else else kw'>else</span>
      <span class='belongs_to identifier id'>belongs_to</span> <span class='association_name identifier id'>association_name</span><span class='comma token'>,</span> <span class='symbol val'>:class_name</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='string val'>&quot;User&quot;</span>
    <span class='end end kw'>end</span>
  <span class='end end kw'>end</span>

  <span class='define_method identifier id'>define_method</span> <span class='symbol val'>:set_creating_user_from_session</span> <span class='do do kw'>do</span>
    <span class='self self kw'>self</span><span class='dot token'>.</span><span class='send identifier id'>send</span><span class='lparen token'>(</span><span class='dstring node'>&quot;#{association_name}=&quot;</span><span class='comma token'>,</span> <span class='user_from_session identifier id'>user_from_session</span><span class='rparen token'>)</span> <span class='unless unless_mod kw'>unless</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='send identifier id'>send</span><span class='lparen token'>(</span><span class='dstring node'>&quot;#{association_name}&quot;</span><span class='rparen token'>)</span>
  <span class='end end kw'>end</span>

  <span class='define_method identifier id'>define_method</span> <span class='symbol val'>:created_by?</span> <span class='do do kw'>do</span> <span class='bitor op'>|</span><span class='user identifier id'>user</span><span class='bitor op'>|</span>
    <span class='return return kw'>return</span> <span class='false false kw'>false</span> <span class='unless unless_mod kw'>unless</span> <span class='user identifier id'>user</span>
    <span class='self self kw'>self</span><span class='dot token'>.</span><span class='send identifier id'>send</span><span class='lparen token'>(</span><span class='association_name identifier id'>association_name</span><span class='rparen token'>)</span><span class='dot token'>.</span><span class='id identifier id'>id</span> <span class='eq op'>==</span> <span class='user identifier id'>user</span><span class='dot token'>.</span><span class='id identifier id'>id</span>
  <span class='end end kw'>end</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="user_from_session-instance_method">
  
    - (<tt>Object</tt>) <strong>user_from_session</strong> 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Returns the currently logged in user via the Authlogic UserSession class.
</p>


  </div>
</div>
<div class="tags">
  
</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


104
105
106
107
108</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/master_may_i/active_record_extensions.rb', line 104</span>

<span class='def def kw'>def</span> <span class='user_from_session identifier id'>user_from_session</span>
  <span class='UserSession constant id'>UserSession</span><span class='dot token'>.</span><span class='find identifier id'>find</span> <span class='andop op'>&amp;&amp;</span> <span class='UserSession constant id'>UserSession</span><span class='dot token'>.</span><span class='find identifier id'>find</span><span class='dot token'>.</span><span class='user identifier id'>user</span>
<span class='rescue rescue kw'>rescue</span> <span class='Authlogic constant id'>Authlogic</span><span class='colon2 op'>::</span><span class='Session constant id'>Session</span><span class='colon2 op'>::</span><span class='Activation constant id'>Activation</span><span class='colon2 op'>::</span><span class='NotActivatedError constant id'>NotActivatedError</span>
  <span class='nil nil kw'>nil</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>
    
    <div id="footer">
  Generated on Wed Dec 23 03:17:30 2009 by 
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool">yard</a>
  0.4.0 (ruby-1.8.6).
</div>

  </body>
</html>